/*
 * YuebaoBuffer.cpp
 *
 *  Created on: 2011-3-19
 *      Author: 
 */

#include "YuebaoBuffer8.h"

YuebaoBufferBase8::YuebaoBufferBase8( int length ) :
    _bufferLength( length ), _bufferPtr( 0 )
{

}
YuebaoBufferBase8::~YuebaoBufferBase8( )
{
    delete _bufferPtr;
    _bufferPtr = NULL;
}

void YuebaoBufferBase8::SetMaxLength( int length )
{
    if( length < 1 )
    {
        return;
    }
    if( _bufferPtr )
    {
        if( _bufferPtr->Des( ).MaxLength( ) < length )
        {
            _bufferPtr = _bufferPtr->ReAlloc( length );
            _bufferLength = length;
        }
    }
    else
    {
        _bufferLength = length;
        _bufferPtr = HBufC8::NewL( _bufferLength );
    }
}

HBufC8* YuebaoBufferBase8::Buffer( )
{
    if( !_bufferPtr )
    {
        _bufferPtr = HBufC8::NewL( _bufferLength );
    }
    return _bufferPtr;
}

const TDesC8& YuebaoBufferBase8::operator()( )
{
    return *Buffer( );
}

TPtr8 YuebaoBufferBase8::Des( )
{
    return Buffer( )->Des( );
}
int YuebaoBufferBase8::Length( )
{
    if( !_bufferPtr )
    {
        return 0;
    }
    else
    {
        return _bufferPtr->Length( );
    }
}
void YuebaoBufferBase8::Zero( )
{
    if( _bufferPtr )
    {
        _bufferPtr->Des( ).Zero( );
    }
}
void YuebaoBufferBase8::Copy( const TDesC8& aDes )
{
    if( !_bufferPtr )
    {
        _bufferPtr = HBufC8::NewL( _bufferLength );
    }
    int appendLength = aDes.Length( );
    if( appendLength > _bufferPtr->Des( ).MaxLength( ) )
    {
        _bufferPtr = _bufferPtr->ReAlloc( appendLength );
    }

    _bufferPtr->Des( ).Copy( aDes );
}
void YuebaoBufferBase8::Copy( const TDesC16& aDes )
{
    if( !_bufferPtr )
    {
        _bufferPtr = HBufC8::NewL( _bufferLength );
    }
    int appendLength = aDes.Length( );
    if( appendLength > _bufferPtr->Des( ).MaxLength( ) )
    {
        _bufferPtr = _bufferPtr->ReAlloc( appendLength );
    }

    _bufferPtr->Des( ).Copy( aDes );
}
void YuebaoBufferBase8::Append( TChar ch )
{
    if( !_bufferPtr )
    {
        _bufferPtr = HBufC8::NewL( _bufferLength );
    }

    int appendLength = 1;
    if( _bufferPtr->Length( ) + appendLength > _bufferPtr->Des( ).MaxLength( ) )
    {
        _bufferPtr = _bufferPtr->ReAlloc( _bufferPtr->Length( ) + appendLength );
    }

    _bufferPtr->Des( ).Append( ch );
}

void YuebaoBufferBase8::Append( const TDesC8 &aDes )
{
    if( !_bufferPtr )
    {
        _bufferPtr = HBufC8::NewL( _bufferLength );
    }

    int appendLength = aDes.Length( );
    if( _bufferPtr->Length( ) + appendLength > _bufferPtr->Des( ).MaxLength( ) )
    {
        _bufferPtr = _bufferPtr->ReAlloc( _bufferPtr->Length( ) + appendLength );
    }

    _bufferPtr->Des( ).Append( aDes );
}

void YuebaoBufferBase8::Append( const TDesC &aDes )
{
    if( !_bufferPtr )
    {
        _bufferPtr = HBufC8::NewL( _bufferLength );
    }

    int appendLength = ( aDes.Length( ) << 1 );
    if( _bufferPtr->Length( ) + appendLength > _bufferPtr->Des( ).MaxLength( ) )
    {
        _bufferPtr = _bufferPtr->ReAlloc( _bufferPtr->Length( ) + appendLength );
    }

    _bufferPtr->Des( ).Append( aDes );
}

void YuebaoBufferBase8::AppendNum( TInt64 number )
{
    if( !_bufferPtr )
    {
        _bufferPtr = HBufC8::NewL( _bufferLength );
    }

    int appendLength = 32;

    if( _bufferPtr->Length( ) + appendLength > _bufferPtr->Des( ).MaxLength( ) )
    {
        _bufferPtr = _bufferPtr->ReAlloc( _bufferPtr->Length( ) + appendLength );
    }
    _bufferPtr->Des( ).AppendNum( number );
}

int YuebaoBufferBase8::Compare( const TDesC8& aDes )
{
    if( _bufferPtr )
    {
        return _bufferPtr->Compare( aDes );
    }
    return -1;
}

int YuebaoBufferBase8::Find( const TDesC8& aDes )
{
    if( _bufferPtr )
    {
        return _bufferPtr->Find( aDes );
    }
    return -1;
}
int YuebaoBufferBase8::Locate( TChar ch )
{
    if( _bufferPtr )
    {
        return _bufferPtr->LocateReverse( ch );
    }
    return -1;
}
int YuebaoBufferBase8::LocateReverse( TChar ch )
{
    if( _bufferPtr )
    {
        return _bufferPtr->LocateReverse( ch );
    }
    return -1;
}
TPtrC8 YuebaoBufferBase8::Left( int length )
{
    if( _bufferPtr )
    {
        return _bufferPtr->Left( length );
    }
    else
    {

        return KNullDesC8( );//TPtrC;
    }
}

TPtrC8 YuebaoBufferBase8::Right( int length )
{
    if( _bufferPtr )
    {
        return _bufferPtr->Right( length );
    }
    else
    {

        return KNullDesC8( );//TPtrC;
    }
}
TPtrC8 YuebaoBufferBase8::Mid( int pos )
{
    if( _bufferPtr )
    {
        return _bufferPtr->Mid( pos );
    }
    else
    {

        return KNullDesC8( );//TPtrC;
    }
}
TPtrC8 YuebaoBufferBase8::Mid( int pos, int length )
{
    if( _bufferPtr )
    {
        return _bufferPtr->Mid( pos, length );
    }
    else
    {

        return KNullDesC8( );//TPtrC;
    }
}
void YuebaoBufferBase8::Format( TRefByValue< const TDesC8 > aFmt, ... )
{
    if( !_bufferPtr )
    {
        _bufferPtr = HBufC8::NewL( _bufferLength );
    }

    VA_LIST ap;
    VA_START(ap,aFmt);
    _bufferPtr->Des( ).FormatList( aFmt, ap );
    VA_END(ap);
}
